package com.egreen.pointsmall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.egreen.common.pojo.entity.pointsmall.Cart;
import com.egreen.common.pojo.entity.pointsmall.PointsUser;
import com.egreen.pointsmall.service.ICartService;
import com.egreen.pointsmall.service.IProductCategoryService;
import com.egreen.pointsmall.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author yyf
 * @since 2023-04-04
 */
@Controller
@RequestMapping("/productCategory")
public class ProductCategoryController {
    @Autowired
    private IProductCategoryService categoryService;
    @Autowired
    private ICartService cartService;
    /**
     * 首页信息的加载
     * @return
     */
    @GetMapping("/main")
    public ModelAndView main(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("main");
        //封装商品分类菜单
        modelAndView.addObject("list", categoryService.buildProductCategoryMenu());
        modelAndView.addObject("levelOneProductList", categoryService.findAllProductByCategoryLevelOne());
        //判断是否为登录用户
        PointsUser pointsUser = (PointsUser) session.getAttribute("user");
        if (pointsUser == null) {
            //表示当前未登录
            modelAndView.addObject("cartList", new ArrayList<>());
        } else {
            modelAndView.addObject("cartList",cartService.getCartVoList(pointsUser.getId()));
        }
        return modelAndView;
    }

}

